var exps={
        user:/^\w{2,20}$/,
        user2:/^[a-zA-Z0-9\u4e00-\u9fa5\_]{2,20}$/,
        user3:/^[a-zA-Z0-9\u4e00-\u9fa5\_]{2,20}|[\w-]+@[\w-]+(?:\.[\w-][\w-]+)+$/,
        password:/^[\w~!@#$%^&*()+{}:"<>?\-=\[\];\',.\/]{6,20}$/,
        email:/^[\w-]+@[\w-]+(?:\.[\w-][\w-]+)+$/,
        phone:/^(?:(?:(?:13[0-9]{1})|159|153)\d{8})$|^[\d-]{10,13}$/,
        num:/^[1-9][0-9.]*$/,
        num2:/^[0-9.]+$/,
        reg:/^[a-zA-Z0-9\-_]{2,20}$/,
        word:/^[a-zA-Z]+$/,
        word2:/^[a-zA-Z0-9]{3,}$/,
        mobile:/^(?:(?:13[0-9]{1})|159|153)\d{8}$/,
        num3:/^[0-9]{4,8}$/,
        num4:/^[1-9]{1}[0-9]{5}$/,
        num5:/^[1-9][0-9]*$/
    };

(function(){
    var side=$(".uside"),
	    main=$(".umain");
    var btn = $('a.chgpsw'), emailBtn = $('a.send-email'), resetBtn = $('a.reset-btn'), verifyBtn = $('.sendVerify');
    
    
	validatePassword($('#oldPassword'), btn, valipsw, '密码格式错误', '原密码');
	validatePassword($('#newPassword'), btn, valipsw, '密码格式错误', '新密码');
	validatePassword($('#newConfirmPassword'), btn, valipsw, '密码格式错误', '再次输入密码');
	
	validatePassword($('#password'), resetBtn, valipsw, '密码格式错误', '新密码');
	validatePassword($('#confirmPassword'), resetBtn, valipsw, '密码格式错误', '再次输入密码');
	validateEmail($('#email'), emailBtn, valiEmail, '邮箱格式错误', '邮箱');
	validatePhone($('#phone'), verifyBtn, valiPhone, '手机号码错误', '手机号码');
	var method=$(".umain .method");
	
	var img_btn_reset = $("#img_btn_reset");
	img_btn_reset.attr("src", "http://ucenter.ehaoyao.com/showValidateCode.json?" + Math.random());
	img_btn_reset.on("click", function () {
		img_btn_reset.attr("src", "http://ucenter.ehaoyao.com/showValidateCode.json?" + Math.random());
	});
	
	$(".icobox li a").on("click", function(){
		var _this=$(this),
		    index=_this.parent().index();
		method.hide().eq(index).show();
		if(_this.hasClass("icon-mail")){
		    _this.addClass("icon-mail2");
			_this.parent().next().children("a").removeClass("icon-phone2");
			_this.parent().next().next().children("a").removeClass("icon-key2");
		}else if(_this.hasClass("icon-phone")){
			_this.addClass("icon-phone2");
			_this.parent().prev().children("a").removeClass("icon-mail2");
			_this.parent().next().children("a").removeClass("icon-key2");
		}else{
			_this.addClass("icon-key2");
		    _this.parent().prev().prev().children("a").removeClass("icon-mail2");
			_this.parent().prev().children("a").removeClass("icon-phone2");
		}
	});

	function sendEmail() {
		var email = $('#email').val();
		if (!exps.email.test(email)) {
			_alert("邮箱格式不正确");
			return;
		}
		$(".submit-btn-red send-email").attr({ "disabled": "disabled" });
		$('#send').attr('class', 'back');
		$('#sending').attr('class', 'fore');
		$.post("/user/forget/findByEmail.json",{email:email},function(result){
		    if(result) {
		    	if (result.code == 1) {
		    		$('#emailTip').removeAttr('style');
		    	} else {
		    		_alert(result.tip);
		    	}
		    } else {
		    	_alert("请求失败");
		    }
		    $(".submit-btn-red send-email").removeAttr("disabled");
			$('#send').attr('class', 'fore');
			$('#sending').attr('class', 'back');
		});
	}
	
	function reset() {
		$("#resetPost").attr({ "disabled": "disabled" });
		$.ajax({
	        type: "POST",
	        url: "/user/resetPw.json?r=" + Math.random(),
	        contentType: "application/x-www-form-urlencoded; charset=utf-8",
	        data: $("#formReset").serialize(),
			dataType:"json",
	        success: function (result) {
	            if (result) {
	                var obj = eval(result);
	                if (obj.code != 1) {
	                    _alert(obj.tip);
	                    $("#resetPost").removeAttr("disabled");
	                    return;
	                }
	            	window.location = "${MALL_WEBSITE}";
	            } else {
	            	_alert("修改密码失败");
	            	 $("#resetPost").removeAttr("disabled");
	            }
	        }
	    });
	}
	
	function sendCode() {
		var phone = $('#phone').val();
		var imgCode = $('#imgCode').val();
		if(imgCode == '' || imgCode == null) {
			_alert("请输入验证码");
			return;
		}
		if(!exps.phone.test(phone)) {
			_alert("请输入正确的手机号码");
			return;
		}
		$(".submit-btn-red").attr({ "disabled": "disabled" });
		$('#sendCode').attr('class', 'back');
		$('#sendCoding').attr('class', 'fore');
		$.post("/send/resetCode.json",{phone:phone, imgCode:imgCode},function(result){
		    if(result) {
		    	if (result.code == 1) {
		    		$('#phoneTip').removeAttr('style');
		    		$('#fixPw').removeAttr('style');
		    		$('#userId').val(result.userId);
		    	} else {
		    		_alert(result.tip);
		    	}
		    } else {
		    	_alert("请求失败");
		    }
		    $(".submit-btn-red").removeAttr("disabled");
			$('#sendCode').attr('class', 'fore');
			$('#sendCoding').attr('class', 'back');
		});
	}
	
	function resetByCode() {
		var userId = $('#userId').val();
		var phone = $('#phone').val();
		if(!exps.phone.test(phone)) {
			_alert("请输入正确的手机号码");
			return;
		}
		var key = $('#key').val();
		var password = $('#password').val();
		if(!exps.password.test(password)) {
			_alert("密码格式不正确");
			return;
		}
		var confirmPassword = $('#confirmPassword').val();
		if(!exps.password.test(confirmPassword)) {
			_alert("密码格式不正确");
			return;
		}
		$(".submit-btn-red reset-btn").attr({ "disabled": "disabled" });
		$('#reset').attr('class', 'back');
		$('#reseting').attr('class', 'fore');
		$.post("/user/resetPw/ByPhone.json",{userId:userId, phone:phone, key:key, password:password, confirmPassword:confirmPassword},function(result){
		    if(result) {
		    	if (result.code == 1) {
		    		_alert("重置密码成功，请重新登录");
		    	} else {
		    		_alert(result.tip);
		    	}
		    } else {
		    	_alert("请求失败");
		    }
		    $(".submit-btn-red reset-btn").removeAttr("disabled");
			$('#reset').attr('class', 'fore');
			$('#reseting').attr('class', 'back');
		});
	}
	
	function updatePw() {
		var oldPassword = $('#oldPassword').val();
		if(!exps.password.test(oldPassword)) {
			_alert("密码格式不正确");
			return;
		}
		var newPassword = $('#newPassword').val();
		if(!exps.password.test(newPassword)) {
			_alert("密码格式不正确");
			return;
		}
		var newConfirmPassword = $('#newConfirmPassword').val();
		if(!exps.password.test(newConfirmPassword)) {
			_alert("密码格式不正确");
			return;
		}
		$(".submit-btn-red confirm-btn").attr({ "disabled": "disabled" });
		$.post("/user/updatePw.json",{oldPassword:oldPassword, newPassword:newPassword, newConfirmPassword:newConfirmPassword},function(result){
		    if(result) {
		    	if (result.code == 1) {
		    		_alert("修改密码成功");
		    		window.location.reload();
		    	} else {
		    		_alert(result.tip);
		    	}
		    } else {
		    	_alert("请求失败");
		    }
		    $(".submit-btn-red confirm-btn").removeAttr("disabled");
		});
	}
	
	function reset() {
		$("#resetPost").attr({ "disabled": "disabled" });
		$.ajax({
            type: "POST",
            url: "/user/resetPw.json?r=" + Math.random(),
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: $("#formReset").serialize(),
			dataType:"json",
            success: function (result) {
                if (result) {
                    var obj = eval(result);
                    if (obj.code != 0) {
                        _alert(obj.tip);
                        $("#resetPost").removeAttr("disabled");
                        return;
                    }
                	window.location = "${MALL_WEBSITE}";
                } else {
                	_alert("修改密码失败");
                	 $("#resetPost").removeAttr("disabled");
                }
            }
        });
	}
	
	/*function valipsw() {
		if(btn.data("permit") == 1) {
			var oldPassword = $('#oldPassword').val();
			var newPassword = $('#newPassword').val();
			var newConfirmPassword = $('#newConfirmPassword').val();
			if(newConfirmPassword != newPassword) {
				_alert("两次输入的密码不一致");
				return;
			}
			if(oldPassword == newPassword) {
				_alert("新密码与旧密码不能相同，请确认");
				return;
			}
			updatePw();
		} else {
			_alert("密码校验不通过，请确认");
		}
	}*/
	
	/**
	 * 校验密码
	 */
	function valipsw() {
		if(btn.data("permit") == 1) {
			var oldPassword = $('#oldPassword').val();
			var newPassword = $('#newPassword').val();
			var newConfirmPassword = $('#newConfirmPassword').val();
			if(newConfirmPassword != newPassword) {
				_alert("两次输入的密码不一致");
				return;
			}
			if(oldPassword == newPassword) {
				_alert("新密码与旧密码不能相同，请确认");
				return;
			}
			updatePw();
		} else if(resetBtn.data("permit") == 1){
			if(verifyBtn.data("permit") == 1) {
				resetByCode();
			} else {
				reset();
			}
		} else {
			_alert("密码校验不通过，请确认");
		}
	}
	
	function valiEmail() {
		if(emailBtn.data("permit") == 1) {
			sendEmail();
		} else {
			_alert("密码校验不通过，请确认");
		}
	}
	
	function valiPhone() {
		if(verifyBtn.data("permit") == 1) {
			sendCode();
		} else {
			_alert("手机号码校验不通过，请确认");
		}
	}
})();